<template>
  <div class="protocolList">
    <search-block>
      <template slot="button">
        <el-button type="primary" @click="search(true)">查询</el-button>
        <el-button type="primary" @click="addProtocol">添加协议</el-button>
      </template>
      <el-form inline :model="model" label-width="100px" @submit.native.prevent>
        <el-form-item label="协议类型:">
          <y-code-select
            v-model="model.agreementType"
            dict="AGREEMENT_TYPE"></y-code-select>
        </el-form-item>
      </el-form>
    </search-block>
    <y-table
      ref="table"
      border
      pagination
      :getDataWay="getDataWay"
      :columns="columns"
    >
      <el-table-column slot="content" label="协议内容" header-align="center" min-width="300">
        <template v-slot="{row}">
          <div class="rich-content" v-html="row.content"></div>
        </template>
      </el-table-column>
      <el-table-column slot="agreementStatus" label="发布" align="center" header-align="center">
        <template v-slot="{row}">
          <el-popconfirm
            :title="row.agreementStatus === '1' ? `是否确认发布？` : `是否确认废弃？`"
            @confirm="doEnable(row, row.agreementStatus === '1')"
          >
            <el-switch
              disabled
              slot="reference"
              v-model="row.agreementStatus"
              active-value="0"
              inactive-value="1"></el-switch>
          </el-popconfirm>
        </template>
      </el-table-column>
      <el-table-column slot="y-oper" label="操作" align="center" header-align="center" width="150">
        <template v-slot="{row}">
          <div class="table-buttons">
            <el-button size="mini" @click="showReadPerson(row)">日志</el-button>
            <el-button size="mini" @click="editProtocol(row)">查看</el-button>
<!--            <el-button v-else type="primary" size="mini" @click="editProtocol(row)">编辑</el-button>-->
<!--            <el-popconfirm
              title="是否确认删除该协议？"
              @confirm="delProtocol(row)">
              <el-button slot="reference" type="danger" size="mini">删除</el-button>
            </el-popconfirm>-->
          </div>
        </template>
      </el-table-column>
    </y-table>
    <y-form
      v-model="forms.formModel"
      :visible.sync="forms.showForm"
      :title="forms.formTypeTitle[forms.formsType]"
      destroy-on-close
      width="900px"
      :form-attrs="formAttrs"
      :columns="formColumns"
      :show-button="forms.formsType === 'ADD'"
      @ensure="ensureModel">
    </y-form>
    <el-dialog
      :visible.sync="readForms.showForm"
      :title="`【${readForms.title}】已读用户`"
      destroy-on-close
      width="50%"
      @opened="onOpenReadPerson"
      @close="hideReadPerson">
      <y-table
        ref="readTable"
        border
        pagination
        :getDataWay="readDataWay"
        :columns="readColumns"></y-table>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "protocolList",
  inject: ['getDictNameSync'],
  computed:{
    getDataWay(){
      return{
        dataType: 'url',
        data: this.$api.applet.protocolList,
        param: this.model
      }
    },
    readDataWay(){
      return{
        dataType: 'url',
        data: this.$api.applet.readPersonList,
        param: {
          id: this.readForms.row.id
        }
      }
    },
    formColumns(){
      return [
        {
          label: "协议类型",
          prop: "agreementType",
          comp:{
            compType: 'YCodeSelect',
            attrs: {
              dict: 'AGREEMENT_TYPE',
              disabled: this.forms.currentForm && this.forms.currentForm.agreementStatus === '0'
            }
          }
        },{
          label: "协议内容",
          prop: "content",
          comp:{
            compType: 'YRichText',
            attrs: {
              businessSysModuleCode: 'AGREEMENT',
              img: false,
              disabled: this.forms.formsType === 'EDIT'
            }
          }
        }
      ]
    },
    formAttrs(){
      return {
        labelWidth: '100px',
        rules: {
          agreementType: [{required: true, message: "请选择协议类型", trigger: "change"}],
          content: [{required: true, message: "请输入协议内容", trigger: "blur"}]
        },
        disabled: this.forms.formsType === 'EDIT'
      }
    }
  },
  data(){
    return{
      model:{
        agreementType: '',
      },
      columns: [{
        label: "协议类型",
        prop: "agreementType",
        width: 100,
        formatter: (function(row){
          return this.getDictNameSync(row.agreementType, 'AGREEMENT_TYPE')
        }).bind(this)
      },{
        label: "协议内容",
        prop: "content",
        minWidth: 300,
        template: true
      },{
        label: "发布",
        prop: "agreementStatus",
        template: true
      }],
      forms:{
        formsType: '',
        formTypeTitle: {
          'ADD': '添加协议',
          'EDIT': '查看协议'
        },
        formModel:{
          agreementType: '',
          content: ''
        },
        currentForm: null,
        showForm: false
      },
      readForms:{
        row: {},
        title: '',
        showForm: false,
      },
      readColumns: [
        {
          label: "阅读人",
          prop: "nickName"
        },
        {
          label: "阅读时间",
          prop: "createTime",
          formatter(row){
            return (row.createTime || '').replace('T', ' ');
          }
        }
      ]
    }
  },
  methods:{
    resetForm(){
      Object.keys(this.forms.formModel).map(key => {
        this.forms.formModel[key] = this.forms.currentForm ? this.forms.currentForm[key] : '';
      })
    },
    addProtocol(){
      this.forms.formsType = 'ADD';
      this.forms.currentForm = null;
      this.resetForm();
      this.forms.showForm = true;
    },
    search(jumpToFirst){
      this.$refs['table'].loadData(this.model, jumpToFirst);
    },
    editProtocol(row){
      this.forms.formsType = 'EDIT';
      this.forms.currentForm = row;
      this.resetForm();
      this.forms.showForm = true;
    },
    delProtocol(row){
      this.$yAxios({
        url: this.$api.applet.removeProtocol,
        data:{
          ids: [row.id]
        }
      }, info => {
        this.$message({
          type: 'success',
          message: '删除成功'
        });
        this.search();
      })
    },
    ensureModel(form){
      let newForm;
      if(this.forms.formsType === 'EDIT'){
        newForm = Object.assign({}, this.forms.currentForm, form);
      }else{
        newForm = Object.assign({ agreementStatus: '1' }, form);
      }
      this.$yAxios({
        url: this.$api.applet.saveProtocol,
        data: newForm
      }, info => {
        this.$message({
          type: 'success',
          message: '保存成功'
        });
        this.search(this.forms.formsType === 'ADD');
      })
    },
    showReadPerson(row){
      this.readForms.row = row;
      this.readForms.title = this.getDictNameSync(row.agreementType, 'AGREEMENT_TYPE');
      this.$nextTick(() => {
        this.readForms.showForm = true;
      })
    },
    onOpenReadPerson(){
      this.$refs['readTable'].loadData(this.readDataWay.param);
    },
    hideReadPerson(){
      this.readForms.row = {};
      this.readForms.title = '';
      this.readForms.showForm = false;
    },
    doEnable(row, bool){
      let url = this.$api.applet.updateProtocolStatus;
      this.$yAxios({
        url,
        data:{
          ids: [row.id],
          status: bool ? '0' : '1',
          agreementType: row.agreementType
        },
        loading: true
      }, info => {
        this.$message({
          type: 'success',
          message: bool ? '发布成功' : '禁用成功'
        });
        this.search();
      })
    }
  }
}
</script>

<style scoped lang="less">
.protocolList{
  /deep/ .rich-content{
    max-height: 150px;
    overflow: auto;
    >p{
      margin: 0
    }
  }
}
</style>